package com.example.mywiseelves.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mywiseelves.R;
import com.example.mywiseelves.dao.ElvesDao;
import com.example.mywiseelves.dao.ResourcesDao;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.dao.VoicesDao;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 进入启动页
 * */
public class SplashActivity extends AppCompatActivity  implements View.OnClickListener{
    ImageView splash;
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5,2, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(100));
    //用户dao对象
    UserDao userDao;
    //精灵dao对象
    ElvesDao elvesDao;
    //资源dao对象
    ResourcesDao resourcesDao;
    //语音dao对象
    VoicesDao voicesDao;
    TextView ignoreTv;
    boolean igonre=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        resourcesDao=new ResourcesDao(this);
        userDao = new UserDao(this);
        elvesDao = new ElvesDao(this);
        resourcesDao = new ResourcesDao(this);
        voicesDao = new VoicesDao(this);
        initData();
        findViews();
        setViews();
        setListener();
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000); // 休眠5秒
                    if (!igonre){
                        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);//启动MainActivity
                        startActivity(intent);
                        finish();//关闭当前活动
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    /**
     * 初始化数据库数据
     */
    private void initData() {
        if (userDao.userQuery().getUser_name() == null) {
            userDao.insertData();
        }
        if (elvesDao.allElvesQuery().size() == 0) {
            elvesDao.insertData(0, "皮卡丘", 0, 0, "这是一只皮卡丘", R.drawable.pikaqiu, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 60);
            elvesDao.insertData(1, "汤姆猫", 0, 0, "这是一只汤姆猫", R.drawable.tomcat, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 50);
            elvesDao.insertData(2, "水晶", 0, 0, "这是一只水晶", R.drawable.exit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 40);
            elvesDao.insertData(3, "可达鸭", 0, 0, "这是一只可达鸭", R.drawable.kedaya, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);
            elvesDao.insertData(4, "兔八哥", 0, 0, "这是一只兔八哥", R.drawable.rabbit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(5, "哆啦A梦", 0, 0, "这是一只哆啦A梦", R.drawable.duola, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);

            elvesDao.insertData(6, "哆啦A梦", 0, 0, "这是一只哆啦A梦", R.drawable.duola, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);
            elvesDao.insertData(7, "兔八哥", 0, 0, "这是一只兔八哥", R.drawable.rabbit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 60);
            elvesDao.insertData(8, "可达鸭", 0, 0, "这是一只可达鸭", R.drawable.kedaya, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(9, "水晶", 0, 0, "这是一只水晶", R.drawable.exit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(10, "汤姆猫", 0, 0, "这是一只汤姆猫", R.drawable.tomcat, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 60);
            elvesDao.insertData(11, "皮卡丘", 0, 0, "这是一只皮卡丘", R.drawable.pikaqiu, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);

            elvesDao.insertData(12, "水晶", 0, 0, "这是一只水晶", R.drawable.exit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(13, "可达鸭", 0, 0, "这是一只可达鸭", R.drawable.kedaya, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);
            elvesDao.insertData(14, "哆啦A梦", 0, 0, "这是一只哆啦A梦", R.drawable.duola, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(15, "汤姆猫", 0, 0, "这是一只汤姆猫", R.drawable.tomcat, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);
            elvesDao.insertData(16, "皮卡丘", 0, 0, "这是一只皮卡丘", R.drawable.pikaqiu, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 60);
            elvesDao.insertData(17, "兔八哥", 0, 0, "这是一只兔八哥", R.drawable.rabbit, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);

            elvesDao.insertData(18, "汤姆猫", 0, 0, "这是一只汤姆猫", R.drawable.tomcat, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 20);
            elvesDao.insertData(19, "皮卡丘", 0, 0, "这是一只皮卡丘", R.drawable.pikaqiu, R.drawable.pikaqiu_normal, R.drawable.pikaqiu_eat, 0, 90);


        }
        if (resourcesDao.allSplashQuery().size() == 0) {
            resourcesDao.insertData(0, R.drawable.splash, 0);
            resourcesDao.insertData(1, R.drawable.splash2, 1);
            resourcesDao.insertData(2, R.drawable.splash3, 0);
            resourcesDao.insertData(3, R.drawable.splash4, 0);
            resourcesDao.insertData(4, R.drawable.splash5, 0);
            resourcesDao.insertData(5, R.drawable.splash6, 0);
        }
        if (voicesDao.allVoiceQuery().size() == 0) {
            voicesDao.insertData(0, "打开浏览器");
            voicesDao.insertData(1, "拨打电话");
        }
    }

    /**
     * 实例化控件
     * */
    private void findViews() {
        splash=findViewById(R.id.mySplash_cl);
        ignoreTv=findViewById(R.id.ignore_tv);
    }
    /**
     * 设置控件
     * */
    private void setViews() {
        int splashImage= resourcesDao.splashQuery();
        splash.setImageResource(splashImage);
    }
    /**
     * 初始化监听
     * */
    private void setListener() {
        ignoreTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.ignore_tv){
            igonre=true;
            //启动MainActivity
            Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
            startActivity(intent);
            finish();//关闭当前活动
        }
    }
}